{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 404,
   "id": "281962a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.set_printoptions(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 405,
   "id": "96bac5dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 406,
   "id": "1422d736",
   "metadata": {},
   "outputs": [],
   "source": [
    "A=np.random.choice(20,size=(3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 407,
   "id": "a46ea3ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "A=np.random.randn(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 408,
   "id": "9ddc1e2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.688, -0.603,  1.406],\n",
       "       [-1.443, -0.023, -0.012],\n",
       "       [ 0.983,  0.996,  0.3  ]])"
      ]
     },
     "execution_count": 408,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 409,
   "id": "1c254b1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "B=np.random.randn(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 410,
   "id": "741959d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.825,  0.794,  0.883, -0.337],\n",
       "       [ 0.9  ,  0.616, -0.161,  1.024],\n",
       "       [ 0.494, -0.507,  0.586,  1.544],\n",
       "       [ 1.404, -2.326, -1.024, -1.151]])"
      ]
     },
     "execution_count": 410,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 411,
   "id": "9b48bc64",
   "metadata": {},
   "outputs": [],
   "source": [
    "n=3\n",
    "A1=A.copy()\n",
    "for i in range(0,n):\n",
    "    for j in range (i+1,n):\n",
    "        C= -A1[j,i]/A1[i,i]\n",
    "        for k in range (i,n):\n",
    "            A1[j,k]=A1[j,k]+A1[i,k]*C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 412,
   "id": "93d34c0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.688, -0.603,  1.406],\n",
       "       [ 0.   ,  0.492, -1.215],\n",
       "       [ 0.   ,  0.   ,  2.712]])"
      ]
     },
     "execution_count": 412,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 413,
   "id": "f08a8bb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.688, -0.603,  1.406],\n",
       "       [-1.443, -0.023, -0.012],\n",
       "       [ 0.983,  0.996,  0.3  ]])"
      ]
     },
     "execution_count": 413,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 414,
   "id": "932c0a9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "BB=np.random.randn(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 415,
   "id": "da867ae5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.273],\n",
       "       [-0.938],\n",
       "       [-0.415]])"
      ]
     },
     "execution_count": 415,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 416,
   "id": "7f704394",
   "metadata": {},
   "outputs": [],
   "source": [
    "n=3\n",
    "B1=A.copy()\n",
    "B2=BB[:,0].copy()\n",
    "for i in range(0,n):\n",
    "    for j in range (i+1,n):\n",
    "        C= -B1[j,i]/B1[i,i]\n",
    "        B2[j]=B2[j]+C*B2[i]\n",
    "        for k in range (i,n):\n",
    "            B1[j,k]=B1[j,k]+B1[i,k]*C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 417,
   "id": "9f9074a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.688, -0.603,  1.406],\n",
       "       [-1.443, -0.023, -0.012],\n",
       "       [ 0.983,  0.996,  0.3  ]])"
      ]
     },
     "execution_count": 417,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 418,
   "id": "84c23ef1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.688, -0.603,  1.406],\n",
       "       [ 0.   ,  0.492, -1.215],\n",
       "       [ 0.   ,  0.   ,  2.712]])"
      ]
     },
     "execution_count": 418,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 419,
   "id": "92113fa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.273, -2.881,  4.689])"
      ]
     },
     "execution_count": 419,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 420,
   "id": "1f27c11e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 421,
   "id": "547f84e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for i in range(3,-1,-1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 422,
   "id": "20afb422",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0.])"
      ]
     },
     "execution_count": 422,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=np.zeros(3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 423,
   "id": "6c05a3b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(n-1,-1,-1):\n",
    "    sumx=0\n",
    "    for j in range(i+1,n):\n",
    "        sumx=sumx+x[j]*B1[i,j]\n",
    "    x[i]=(B2[i]-sumx)/B1[i,i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 424,
   "id": "f1244be6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.661, -1.59 ,  1.729])"
      ]
     },
     "execution_count": 424,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 425,
   "id": "3a624276",
   "metadata": {},
   "outputs": [],
   "source": [
    "inverseB3=np.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 426,
   "id": "5fd0210f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.002, -0.703, -0.018],\n",
       "       [-0.187,  0.839,  0.911],\n",
       "       [ 0.628, -0.484,  0.369]])"
      ]
     },
     "execution_count": 426,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inverseB3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 427,
   "id": "108a3a84",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1=inverseB3@BB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 428,
   "id": "71af906b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.661],\n",
       "       [-1.59 ],\n",
       "       [ 1.729]])"
      ]
     },
     "execution_count": 428,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 429,
   "id": "36b5a52e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x2=inverseB3@A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 430,
   "id": "ae86bd7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.000e+00, -6.962e-17,  2.152e-16],\n",
       "       [-1.352e-16,  1.000e+00, -3.577e-16],\n",
       "       [-4.065e-17, -5.897e-17,  1.000e+00]])"
      ]
     },
     "execution_count": 430,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
